package com.xy.routerlibrary

import android.app.Activity
import android.content.Intent

/**
 * @ClassName Router
 * @Author xy
 * @Date 2021/7/29 9:30
 * @Description 自定义路由跳转
 */
object Router {

    var groupMap = HashMap<String, HashMap<String, Class<*>>>()

    var routerMap = HashMap<String, Class<*>>()

    /**
     *
     * @param path String 路径 /main/MainActivity
     * @param className Class<*> 类名
     */
    fun register(path: String, className: Class<*>) {
        var strList = path.split("/")
        if (strList.size > 2) {
            var groupName = strList[1]
            var routerName = path

            var group :HashMap<String, Class<*>>?=null
            if (groupMap.containsKey(groupName)){
                 group = groupMap[groupName]
            }

            if (group==null){
                group = HashMap()
                groupMap[groupName] = group
            }

            if (group!=null){
                group[routerName] = className
            }
        }
    }

    /**
     *
     * @param path String
     */
    fun startActivity(activity: Activity,path: String) {
        var strList = path.split("/")
        if (strList.size > 2) {
            var groupName = strList[1]
            var routerName = path

            var group :HashMap<String, Class<*>>?=null
            if (groupMap.containsKey(groupName)){
                group = groupMap[groupName]
            }

            if (group!=null&&group.containsKey(routerName)){
                var cla = group.get(routerName)
                activity.startActivity(Intent(activity,cla))
            }

        }
    }
}